npe on parsing empty/whitespace only geo-rss point element catched
(fixes #27)
This commit is contained in:
parent
49cea1b1c5
commit
8335c6d43a
5 changed files with 1487 additions and 28 deletions
|
@ -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) {
|
||||
|
|
|
@ -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 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);
|
||||
|
|
|
@ -61,9 +61,13 @@ public class W3CGeoParser implements ModuleParser {
|
|||
}
|
||||
if (lat != null && lng != null) {
|
||||
geoRSSModule = new W3CGeoModuleImpl();
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
1401
src/test/resources/data/geo-rss-issue-27.rss
Normal file
1401
src/test/resources/data/geo-rss-issue-27.rss
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue