From 8335c6d43a777e94dfc47c497112fc75bfa5fd86 Mon Sep 17 00:00:00 2001 From: Martin Kurz Date: Fri, 18 Apr 2014 17:51:21 +0200 Subject: [PATCH] npe on parsing empty/whitespace only geo-rss point element catched (fixes #27) --- .../feed/module/georss/GeoRSSUtils.java | 3 + .../feed/module/georss/SimpleParser.java | 10 +- .../feed/module/georss/W3CGeoParser.java | 8 +- .../rometools/feed/module/georss/RssTest.java | 93 +- src/test/resources/data/geo-rss-issue-27.rss | 1401 +++++++++++++++++ 5 files changed, 1487 insertions(+), 28 deletions(-) create mode 100644 src/test/resources/data/geo-rss-issue-27.rss diff --git a/src/main/java/org/rometools/feed/module/georss/GeoRSSUtils.java b/src/main/java/org/rometools/feed/module/georss/GeoRSSUtils.java index 5b94f45..22f57e3 100644 --- a/src/main/java/org/rometools/feed/module/georss/GeoRSSUtils.java +++ b/src/main/java/org/rometools/feed/module/georss/GeoRSSUtils.java @@ -29,6 +29,9 @@ import com.sun.syndication.feed.synd.SyndFeed; public class GeoRSSUtils { static String trimWhitespace(final String in) { + if (in == null) { + return ""; + } final StringBuffer strbuf = new StringBuffer(); int i = 0; for (; i < in.length() && Character.isWhitespace(in.charAt(i)); ++i) { diff --git a/src/main/java/org/rometools/feed/module/georss/SimpleParser.java b/src/main/java/org/rometools/feed/module/georss/SimpleParser.java index a9bf156..5150f78 100644 --- a/src/main/java/org/rometools/feed/module/georss/SimpleParser.java +++ b/src/main/java/org/rometools/feed/module/georss/SimpleParser.java @@ -78,10 +78,12 @@ public class SimpleParser implements ModuleParser { final Element whereElement = element.getChild("where", GeoRSSModule.SIMPLE_NS); if (pointElement != null) { geoRSSModule = new SimpleModuleImpl(); - final String coordinates = pointElement.getText(); - final String[] coord = GeoRSSUtils.trimWhitespace(coordinates).split(" "); - final Position pos = new Position(Double.parseDouble(coord[0]), Double.parseDouble(coord[1])); - geoRSSModule.setGeometry(new Point(pos)); + final String coordinates = GeoRSSUtils.trimWhitespace(pointElement.getText()); + if (!"".equals(coordinates)) { + final String[] coord = coordinates.split(" "); + final Position pos = new Position(Double.parseDouble(coord[0]), Double.parseDouble(coord[1])); + geoRSSModule.setGeometry(new Point(pos)); + } } else if (lineElement != null) { geoRSSModule = new SimpleModuleImpl(); final PositionList posList = parsePosList(lineElement); diff --git a/src/main/java/org/rometools/feed/module/georss/W3CGeoParser.java b/src/main/java/org/rometools/feed/module/georss/W3CGeoParser.java index 58d3c01..fb5c4e1 100644 --- a/src/main/java/org/rometools/feed/module/georss/W3CGeoParser.java +++ b/src/main/java/org/rometools/feed/module/georss/W3CGeoParser.java @@ -61,8 +61,12 @@ public class W3CGeoParser implements ModuleParser { } if (lat != null && lng != null) { geoRSSModule = new W3CGeoModuleImpl(); - final Position pos = new Position(Double.parseDouble(lat.getText()), Double.parseDouble(lng.getText())); - geoRSSModule.setGeometry(new Point(pos)); + final String latTxt = lat.getText(); + final String lngTxt = lng.getText(); + if (!"".equals(latTxt) && !"".equals(lngTxt)) { + final Position pos = new Position(Double.parseDouble(lat.getText()), Double.parseDouble(lng.getText())); + geoRSSModule.setGeometry(new Point(pos)); + } } return geoRSSModule; diff --git a/src/test/java/org/rometools/feed/module/georss/RssTest.java b/src/test/java/org/rometools/feed/module/georss/RssTest.java index 8338b68..e3eafbb 100644 --- a/src/test/java/org/rometools/feed/module/georss/RssTest.java +++ b/src/test/java/org/rometools/feed/module/georss/RssTest.java @@ -41,23 +41,35 @@ import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.SyndFeedOutput; import com.sun.syndication.io.XmlReader; +/** + * tests for geo-rss module. + * + */ public class RssTest extends TestCase { - private static double DELTA = 0.00001; + private static final double DELTA = 0.00001; /** - * compares expected lat/lng values with acutal values read from feed - * + * compares expected lat/lng values with acutal values read from feed. + * * @param fileName a file in the src/data directory * @param expectedLat array of expected latitude values * @param expectedLng array of expected longitude values - * @throws Exception + * @throws Exception if file not found or not accessible */ - private void assertTestFile(final String fileName, final double[] expectedLat, final double[] expectedLng) throws Exception { + private void assertTestFile(final String fileName, final Double[] expectedLat, final Double[] expectedLng) throws Exception { assertTestInputStream(new FileInputStream(new File("src/test/resources/data/", fileName)), expectedLat, expectedLng); } - private void assertTestInputStream(final InputStream in, final double[] expectedLat, final double[] expectedLng) throws Exception { + /** + * test expected latitude and longitude values in items of test file. + * + * @param in testfeed + * @param expectedLat expected latitude values + * @param expectedLng expected longitude values + * @throws Exception if file not found or not accessible + */ + private void assertTestInputStream(final InputStream in, final Double[] expectedLat, final Double[] expectedLng) throws Exception { final SyndFeedInput input = new SyndFeedInput(); final SyndFeed feed = input.build(new XmlReader(in)); @@ -67,11 +79,18 @@ public class RssTest extends TestCase { final SyndEntry entry = entries.get(i); final GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(entry); final Position position = geoRSSModule.getPosition(); - assertEquals("lat " + i, expectedLat[i], position.getLatitude(), DELTA); - assertEquals("lng " + i, expectedLng[i], position.getLongitude(), DELTA); + if (expectedLat[i] == null || expectedLng[i] == null) { + assertNull("position " + i, position); + } else { + assertEquals("lat " + i, expectedLat[i], position.getLatitude(), DELTA); + assertEquals("lng " + i, expectedLng[i], position.getLongitude(), DELTA); + } } } + /** + * @return implementation of SyndFeed with basic setup + */ private SyndFeed createFeed() { final SyndFeed feed = new SyndFeedImpl(); feed.setFeedType("rss_2.0"); @@ -98,30 +117,54 @@ public class RssTest extends TestCase { return feed; } + /** + * @throws Exception if file not found or not accessible + */ public void testReadRss2Simple() throws Exception { - final double[] lat = { 31.7666667, 31.7666667, 31.7666667 }; - final double[] lng = { 35.2333333, 35.2333333, 35.2333333 }; + final Double[] lat = { 31.7666667, 31.7666667, 31.7666667 }; + final Double[] lng = { 35.2333333, 35.2333333, 35.2333333 }; assertTestFile("rss2.0-simple.xml", lat, lng); } + /** + * @throws Exception if file not found or not accessible + */ public void testReadRss2W3CGeo() throws Exception { - final double[] lat = { 42.986284, 41.492491, 39.901893, 40.750598 }; - final double[] lng = { -71.45156, -72.095783, -75.171998, -73.993392 }; + final Double[] lat = { 42.986284, 41.492491, 39.901893, 40.750598 }; + final Double[] lng = { -71.45156, -72.095783, -75.171998, -73.993392 }; assertTestFile("rss2.0-w3cgeo.xml", lat, lng); } + /** + * @throws Exception if file not found or not accessible + */ public void xtestReadRss1W3CGeo() throws Exception { - final double[] lat = { 45.9774, 45.9707, 46.0087 }; - final double[] lng = { 8.9781, 8.97055, 8.98579 }; + final Double[] lat = { 45.9774, 45.9707, 46.0087 }; + final Double[] lng = { 8.9781, 8.97055, 8.98579 }; assertTestFile("rss1.0-w3cgeo.xml", lat, lng); } + /** + * @throws Exception if file not found or not accessible + */ public void testReadGML() throws Exception { - final double[] lat = { 45.256, 45.256, 45.256, 45.256 }; - final double[] lng = { -71.92, -71.92, -71.92, -71.92 }; + final Double[] lat = { 45.256, 45.256, 45.256, 45.256 }; + final Double[] lng = { -71.92, -71.92, -71.92, -71.92 }; assertTestFile("rss2.0-gml.xml", lat, lng); } + /** + * @throws Exception if file not found or not accessible + */ + public void testNpeOnEmptyPoint() throws Exception { + final Double[] lat = { null, 0.0, 0.0, 0.0, 43.653226, 0.0, 0.0, 0.0, 0.0, 0.0 }; + final Double[] lng = { null, 0.0, 0.0, 0.0, -79.383184, 0.0, 0.0, 0.0, 0.0, 0.0 }; + assertTestFile("geo-rss-issue-27.rss", lat, lng); + } + + /** + * @throws Exception if file not found or not accessible + */ public void xtestProduceAndReadGML() throws Exception { final SyndFeed feed = createFeed(); final GeoRSSModule geoRSSModule = new GMLModuleImpl(); @@ -141,11 +184,14 @@ public class RssTest extends TestCase { output.output(feed, stringWriter); final InputStream in = new ByteArrayInputStream(stringWriter.toString().getBytes("UTF8")); - final double[] lat = { latitude }; - final double[] lng = { longitude }; + final Double[] lat = { latitude }; + final Double[] lng = { longitude }; assertTestInputStream(in, lat, lng); } + /** + * @throws Exception if file not found or not accessible + */ public void testProduceAndReadSimple() throws Exception { final SyndFeed feed = createFeed(); final GeoRSSModule geoRSSModule = new SimpleModuleImpl(); @@ -165,11 +211,14 @@ public class RssTest extends TestCase { output.output(feed, stringWriter); final InputStream in = new ByteArrayInputStream(stringWriter.toString().getBytes("UTF8")); - final double[] lat = { latitude }; - final double[] lng = { longitude }; + final Double[] lat = { latitude }; + final Double[] lng = { longitude }; assertTestInputStream(in, lat, lng); } + /** + * @throws Exception if file not found or not accessible + */ public void testProduceAndReadSimpleLine() throws Exception { SyndFeed feed = createFeed(); @@ -204,8 +253,8 @@ public class RssTest extends TestCase { final LineString lineString = (LineString) geoRSSModule.getGeometry(); positionList = lineString.getPositionList(); - assertEquals(latitudes[i], positionList.getLatitude(i), 0.0001); - assertEquals(longitudes[i], positionList.getLongitude(i), 0.0001); + assertEquals(latitudes[i], positionList.getLatitude(i), DELTA); + assertEquals(longitudes[i], positionList.getLongitude(i), DELTA); } } diff --git a/src/test/resources/data/geo-rss-issue-27.rss b/src/test/resources/data/geo-rss-issue-27.rss new file mode 100644 index 0000000..e0ce403 --- /dev/null +++ b/src/test/resources/data/geo-rss-issue-27.rss @@ -0,0 +1,1401 @@ + + + + + The Torontonian + + http://thetorontonian.ca + A Lifestyle Blog + Tue, 15 Apr 2014 20:19:46 +0000 + en + hourly + 1 + http://wordpress.com/ + + + http://0.gravatar.com/blavatar/81b7bf46fd5aa138012ed7156d139088?s=96&d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png + The Torontonian + http://thetorontonian.ca + + +